package com.example.yvque0312.controller;

import com.aliyun.oss.OSSClient;
import com.example.yvque0312.pojo.User;
import com.example.yvque0312.service.UserService;
import com.example.yvque0312.util.Code;
import com.example.yvque0312.util.MD5Uitl;
import com.example.yvque0312.util.OSSUtil;
import com.example.yvque0312.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private OSSUtil ossUtil;
    @RequestMapping("/zhuce")
    public Result zhuce(@RequestBody User user){

        User xx = userService.login(user);
        if(xx!=null){
            return new Result(Code.GET_ERR,"用户已存在");
        }else{
            int i = userService.zhuce(user);
            if(i==1){
                return new Result(Code.SAVE_OK,"注册成功");
            }else{
                return new Result(Code.SAVE_ERR,"该用户已存在");
            }
        }

    }
    //登录

    @RequestMapping("/denglu")
    public Result denglu(@RequestBody User user){
        User xx = userService.login(user);
        if(xx==null){
            return new Result(Code.GET_ERR,"用户不存在");
        }else{
            String encrypt = MD5Uitl.encrypt(user.getPassword());
            if ( encrypt.equals(xx.getPassword())){
                return new Result(Code.GET_OK,xx,"登录成功");
            }else {
                return new Result(Code.GET_ERR,"密码错误");
            }
        }
    }
    //编辑查全部
    @RequestMapping("/cha/{id}")
    public Result cha(@PathVariable("id") Integer id){
        User xx = userService.cha(id);
        if(xx!=null){
            return new Result(Code.GET_OK,xx,"查询成功");
        }else{
            return new Result(Code.GET_ERR,xx,"查询失败");
        }
    }
    //修改个人信息
    @RequestMapping("/xiugai")
    public Result xiugai(@RequestBody User user){
        int xx = userService.xiugai(user);
        if(xx>0){
            return new Result(Code.GET_OK,"修改成功");
        }else{
            return new Result(Code.GET_ERR,"修改失败");
        }
    }
    //上传头像到阿里服务器
    @RequestMapping("/shangchuan")
    public Result upload(MultipartFile file) throws IOException {
        System.out.println(file);
        String originalFilename = file.getOriginalFilename();
        String newname = UUID.randomUUID().toString().substring(24) + "-" + originalFilename;
        OSSClient ossClient = new OSSClient(ossUtil.getUrl(), ossUtil.getId(), ossUtil.getMiyao());
        ossClient.putObject(ossUtil.getTong(), newname, file.getInputStream());
        String url1 = ossUtil.getUrl();
        String[] split = url1.split("//");
        String url = split[0] + "//" + ossUtil.getTong() + "." + split[1] + "/" + newname;
        return new Result(Code.UPDATE_OK, url, "上传成功");
    }
    //修改头像
    @RequestMapping("/xiugaiurl")
    public Result shangchuan(@RequestBody User user){
        int i = userService.shangchaun(user);
        if (i>0){
            return new Result(Code.SAVE_OK,"上传成功");
        }else {
            return new Result(Code.SAVE_ERR,"上传失败");
        }
    }

    //修改密码
    @RequestMapping("/xiugaimima")
    public Result xiugaimima(@RequestBody User user){
        System.out.println("旧密码："+user.getOldPassword());
        System.out.println("新密码："+user.getPassword());
       User xx = userService.cha(user.getId());
        String password = xx.getPassword();
        MD5Uitl md5Uitl = new MD5Uitl();
        String miwen = md5Uitl.encrypt(user.getOldPassword());
        if (password.equals(miwen)){
            int i = userService.xiugaimima(user);
            if (i>0){
                return new Result(Code.SAVE_OK,"修改成功");
            }else {
                return new Result(Code.SAVE_ERR,"修改失败");
            }
        } else {
            return new Result(Code.SAVE_ERR,"密码错误");
        }

    }
}
